diff --git a/src/field/DistributedField.cpp b/src/field/DistributedField.cpp
index 15b8c38105f9ac194e1ae6762b6d206fe1ee22e5..dfdffe7bb54078ec12376842150abc849b788a56 100644
--- a/src/field/DistributedField.cpp
+++ b/src/field/DistributedField.cpp
@@ -413,6 +413,16 @@ namespace gmshfem::field
       unsigned long long global;
     };
 
+    MPI_Datatype types[3] = {MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG};
+    MPI_Datatype mpi_struct_type;
+    MPI_Aint offsets[3];
+    int blocklengths[3] = {1, 1, 1};
+    offsets[0] = offsetof(LocalGlobalPair, type);
+    offsets[1] = offsetof(LocalGlobalPair, entity);
+    offsets[2] = offsetof(LocalGlobalPair, global);
+    MPI_Type_create_struct(3, blocklengths, offsets, types, &mpi_struct_type);
+    MPI_Type_commit(&mpi_struct_type);
+
     std::vector< LocalGlobalPair > myOwnedDofs, allSharedDofsArray;
 
     for(auto dof : this->_toSend) {
@@ -431,15 +441,7 @@ namespace gmshfem::field
     }
 
 
-    MPI_Datatype types[3] = {MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG};
-    MPI_Datatype mpi_struct_type;
-    MPI_Aint offsets[3];
-    int blocklengths[3] = {1, 1, 1};
-    offsets[0] = offsetof(LocalGlobalPair, type);
-    offsets[1] = offsetof(LocalGlobalPair, entity);
-    offsets[2] = offsetof(LocalGlobalPair, global);
-    MPI_Type_create_struct(3, blocklengths, offsets, types, &mpi_struct_type);
-    MPI_Type_commit(&mpi_struct_type);
+    
     // Synchronize the full array
     allSharedDofsArray.resize(total_size);
 
@@ -451,8 +453,7 @@ namespace gmshfem::field
       msg::info << "Gathered allSharedDofsArray. Total size is " << allSharedDofsArray.size() << msg::endl;
     }
     MPI_Barrier(MPI_COMM_WORLD);
-    // Free the type
-    MPI_Type_free(&mpi_struct_type);
+    
 
     std::unordered_map< DofIndex, unsigned long long, HashBySecond, std::equal_to<DofIndex> > allSharedDofs;
     // Put in hashtable
@@ -488,6 +489,8 @@ namespace gmshfem::field
       msg::debug << msg::endl
                  << "Leaving syncGlobalDofs" << msg::endl;
     MPI_Barrier(MPI_COMM_WORLD);
+    // Free the type
+    MPI_Type_free(&mpi_struct_type);
 
 #else
     throw common::Exception("MPI is not available");