diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp
index acbc93610d077545f6a0ca60d125b1b6bd0f714c..8a82c3fdba171683cf78aed800360066169e963b 100644
--- a/Geo/GModel.cpp
+++ b/Geo/GModel.cpp
@@ -2066,7 +2066,7 @@ int GModel::partitionMesh(
     if(elementPartition.empty())
       return PartitionMesh(this, numPart);
     else
-      return PartitionUsingThisSplit(this, numPart, elementPartition);
+      return PartitionUsingThisSplit(this, elementPartition);
   }
   return 1;
 #else
diff --git a/Mesh/meshPartition.cpp b/Mesh/meshPartition.cpp
index d68c8be891672ee48916d1b05f6b043c9d90aaef..152879bd9912cf88b98110a694637f3ae9b4eb64 100644
--- a/Mesh/meshPartition.cpp
+++ b/Mesh/meshPartition.cpp
@@ -15,6 +15,7 @@
 #include <cstdlib>
 #include <map>
 #include <unordered_map>
+#include <unordered_set>
 #include "GmshConfig.h"
 #include "GmshMessage.h"
 #include "GModel.h"
@@ -2512,11 +2513,19 @@ int UnpartitionMesh(GModel *model)
 
 // Create the partition according to the element split given by elmToPartition
 // Returns: 0 = success, 1 = no elements found.
-int PartitionUsingThisSplit(GModel *model, int numPart,
+int PartitionUsingThisSplit(GModel *model,
                             std::vector<std::pair<MElement *, int> > &elmToPart)
 {
   Graph graph(model);
   if(makeGraph(model, graph, -1)) return 1;
+  
+  int numPart = 0;
+  std::unordered_set< int > partTags;
+  for(std::size_t i = 0; i < elmToPart.size(); i++) {
+    partTags.insert(elmToPart[i].second);
+  }
+  numPart = partTags.size();
+  
   graph.createDualGraph(false);
   graph.nparts(numPart);
 
@@ -2600,7 +2609,7 @@ int ConvertOldPartitioningToNewOne(GModel *model)
     }
   }
 
-  return PartitionUsingThisSplit(model, partitions.size(), elmToPartition);
+  return PartitionUsingThisSplit(model, elmToPartition);
 }
 
 #else
@@ -2616,7 +2625,7 @@ int UnpartitionMesh(GModel *model) { return 0; }
 int ConvertOldPartitioningToNewOne(GModel *model) { return 0; }
 
 int PartitionUsingThisSplit(
-  GModel *model, std::size_t npart,
+  GModel *model,
   std::vector<std::pair<MElement *, int> > &elmToPartition)
 {
   Msg::Error("Gmsh must be compiled with METIS support to partition meshes");
diff --git a/Mesh/meshPartition.h b/Mesh/meshPartition.h
index bf1fc354e4a36a94a322e0c30b963eb222ad2204..22a0a27cdac92998052849c66288962c93678e0f 100644
--- a/Mesh/meshPartition.h
+++ b/Mesh/meshPartition.h
@@ -13,7 +13,7 @@ class GFace;
 int PartitionMesh(GModel *model, int numPart);
 int UnpartitionMesh(GModel *model);
 
-int PartitionUsingThisSplit(GModel *model, int numPart,
+int PartitionUsingThisSplit(GModel *model,
   std::vector<std::pair<MElement *, int> > &elmToPartition);
 int ConvertOldPartitioningToNewOne(GModel *model);
 
diff --git a/Plugin/SimplePartition.cpp b/Plugin/SimplePartition.cpp
index 7868244477299eaac480feb77c50711a012063f0..976a0f359685a87c2b181616021bd528a018a67c 100644
--- a/Plugin/SimplePartition.cpp
+++ b/Plugin/SimplePartition.cpp
@@ -171,8 +171,7 @@ void GMSH_SimplePartitionPlugin::run()
 
   opt_mesh_partition_create_topology(0, GMSH_SET | GMSH_GUI, createTopology);
 
-  int ier = PartitionUsingThisSplit(m, numSlicesX * numSlicesY * numSlicesZ,
-                                    elmToPartition);
+  int ier = PartitionUsingThisSplit(m, elmToPartition);
   if(!ier) {
     opt_mesh_color_carousel(0, GMSH_SET | GMSH_GUI, 3.);
     CTX::instance()->mesh.changed = ENT_ALL;