diff --git a/src/Decomposition/CartDecomposition.hpp b/src/Decomposition/CartDecomposition.hpp
index a3361c0d02036f056ef3f21bbe1bce00be795c75..5f5d75e91355405b006b0841da330be7c20cd0f7 100644
--- a/src/Decomposition/CartDecomposition.hpp
+++ b/src/Decomposition/CartDecomposition.hpp
@@ -106,11 +106,11 @@ private:
 		// Here we use METIS
 
 		// Create a cartesian grid graph
-		CartesianGraphFactory<3,Graph_CSR<nm_part_v,nm_part_e>> g_factory_part;
+		CartesianGraphFactory<dim,Graph_CSR<nm_v,nm_e>> g_factory_part;
 
 		// Processor graph
 
-		Graph_CSR<nm_part_v,nm_part_e> gp = g_factory_part.construct<NO_EDGE,float,2>(div,domain);
+		Graph_CSR<nm_v,nm_e> gp = g_factory_part.template construct<NO_EDGE,T,dim-1,0,1>(div,domain);
 
 		// Get the number of processing units
 		size_t Np = v_cl.getProcessingUnits();
@@ -119,30 +119,41 @@ private:
 		long int p_id = v_cl.getProcessUnitID();
 
 		// Convert the graph to metis
-		Metis<Graph_CSR<nm_part_v,nm_part_e>> met(gp,Np);
+		Metis<Graph_CSR<nm_v,nm_e>> met(gp,Np);
 
 		// decompose
 
-		met.decompose<nm_part_v::id>();
+		met.decompose<nm_v::id>();
 
 		// Optimize the decomposition creating bigger spaces
 		// And reducing Ghost over-stress
 
-		dec_optimizer<3,Graph_CSR<nm_part_v,nm_part_e>> d_o(gp,div);
+		dec_optimizer<dim,Graph_CSR<nm_v,nm_e>> d_o(gp,div);
 
 		// set of Boxes produced by the decomposition optimizer
 		openfpm::vector<::Box<dim,size_t>> loc_box;
 
-		grid_key_dx<3> keyZero(0,0,0);
-		d_o.optimize<nm_part_v::sub_id,nm_part_v::id>(keyZero,gp,p_id,loc_box);
+		// a grig key poiting to the origin
+		grid_key_dx<dim> keyZero;
+		keyZero.zero();
+
+		// optimize the decomposition
+		d_o.template optimize<nm_v::sub_id,nm_v::id>(keyZero,gp,p_id,loc_box);
+
+		//-------------------DEBUG---------
+		VTKWriter<decltype(gp)> vtk(gp);
+		vtk.write("out_graph.vtk");
+		//---------------------------------
+
+		exit(1);
 
 		// convert into sub-domain
 		for (size_t s = 0 ; s < loc_box.size() ; s++)
 		{
 			SpaceBox<dim,T> sub_d(loc_box.get(s));
 
-			// re-scale with the spacing
-			sub_d.mul(spacing);
+			// re-scale with spacing
+			sub_d.spacing(spacing);
 
 			// add the sub-domain
 			sub_domains.add(sub_d);
diff --git a/src/Decomposition/CartDecomposition_unit_test.hpp b/src/Decomposition/CartDecomposition_unit_test.hpp
index 851cd64e0039e2ab9ab56487f231b5efa454f303..a7294215044cc53c45a1e60daea97e4f6a157027 100644
--- a/src/Decomposition/CartDecomposition_unit_test.hpp
+++ b/src/Decomposition/CartDecomposition_unit_test.hpp
@@ -30,8 +30,6 @@ BOOST_AUTO_TEST_CASE( CartDecomposition_test_use)
 
 	// Decompose
 	dec.setParameters(div,box);
-
-
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/Graph/CartesianGraphFactory.hpp b/src/Graph/CartesianGraphFactory.hpp
index e7d56233d5a555c7c3ec497a41ec458eccc6edd6..500ad2c201b703656aa5e7cee998436627c326e8 100644
--- a/src/Graph/CartesianGraphFactory.hpp
+++ b/src/Graph/CartesianGraphFactory.hpp
@@ -364,8 +364,7 @@ public:
 	 *           no property will store this information
 	 * \tparam T type of the domain like (int real complex ... )
 	 * \tparam dim_c Connectivity dimension
-	 * \tparam Memory class that create new memory
-	 * \tparam pos... one or more integer indicating the spatial properties
+	 * \tparam pos... (optional)one or more integer indicating the spatial properties
 	 *
 	 */
 	template <unsigned int se,typename T, unsigned int dim_c, int... pos>
diff --git a/src/Grid/grid_dist_id.hpp b/src/Grid/grid_dist_id.hpp
index 12e53afeccdc99f815d073eb15b4de6cca70bff6..27cf22d1fcf0aa6c6eb13e2b4378397b71384e5e 100644
--- a/src/Grid/grid_dist_id.hpp
+++ b/src/Grid/grid_dist_id.hpp
@@ -7,7 +7,6 @@
 #include "Space/SpaceBox.hpp"
 #include "mathutil.hpp"
 #include "grid_dist_id_iterator.hpp"
-#include "grid_dist_id_iterator_margin.hpp"
 #include "grid_dist_key.hpp"
 
 #define SUB_UNIT_FACTOR 64
@@ -93,7 +92,7 @@ class grid_dist_id
 		for (size_t d = 0 ; d < dim ; d++)
 		{
 			// push the size of the local grid
-			v_size[d] = sp.getHigh(d) - sp.getLow(d);
+			v_size[d] = sp.getHigh(d) - sp.getLow(d) + 1;
 		}
 	}
 
@@ -119,16 +118,36 @@ public:
 
 		// Create the sub-domains
 		dec.setParameters(div);
+
+		// Create local grid
+		Create();
 	}
 
 	//! constructor
 	grid_dist_id(size_t (& g_sz)[dim])
-	:v_cl(*global_v_cluster),dec(Decomposition(v_cl))
+	:dec(Decomposition(*global_v_cluster)),v_cl(*global_v_cluster)
 	{
 		// fill the global size of the grid
 		for (int i = 0 ; i < dim ; i++)	{this->g_sz[i] = g_sz[i];}
 
-		// first compute a decomposition
+		// Get the number of processor and calculate the number of sub-domain
+		// for decomposition
+		size_t n_proc = v_cl.getProcessingUnits();
+		size_t n_sub = n_proc * SUB_UNIT_FACTOR;
+
+		// Calculate the maximum number (before merging) of sub-domain on
+		// each dimension
+		size_t div[dim];
+		for (int i = 0 ; i < dim ; i++)
+		{div[i] = round_big_2(pow(n_sub,1.0/dim));}
+
+		// Box
+		Box<dim,size_t> b(g_sz);
+
+		// Create the sub-domains
+		dec.setParameters(div,b);
+
+		// Create local grid
 		Create();
 	}
 
@@ -193,9 +212,9 @@ public:
 	 * \return An iterator to a grid with specified margins
 	 *
 	 */
-	grid_dist_iterator_margin<dim,device_grid> getBulkIterator(size_t margin)
+	grid_dist_iterator<dim,device_grid> getDomainIterator()
 	{
-		grid_dist_iterator_margin<dim,device_grid> it(loc_grid,margin);
+		grid_dist_iterator<dim,device_grid> it(loc_grid,0);
 
 		return it;
 	}
diff --git a/src/Grid/grid_dist_id_iterator.hpp b/src/Grid/grid_dist_id_iterator.hpp
index 3121c34cfa863cfa8f73d8fb0723bb3f1536cb50..510a69de02f230a37ce3d038cc724e6657d4df2a 100644
--- a/src/Grid/grid_dist_id_iterator.hpp
+++ b/src/Grid/grid_dist_id_iterator.hpp
@@ -1,5 +1,5 @@
 /*
- * grid_dist_id_iterator.hpp
+ * grid_dist_id_iterator_sub.hpp
  *
  *  Created on: Feb 4, 2015
  *      Author: Pietro Incardona
@@ -9,7 +9,7 @@
 #define GRID_DIST_ID_ITERATOR_HPP_
 
 #include "grid_dist_key.hpp"
-#include "Grid/grid.hpp"
+#include "VCluster.hpp"
 
 /*! \brief Distributed grid iterator
  *
@@ -17,18 +17,20 @@
  *
  */
 
-template<unsigned int dim, typename l_grid>
+template<unsigned int dim, typename device_grid>
 class grid_dist_iterator
 {
 	//! grid list counter
-
 	size_t g_c;
 
 	//! List of the grids we are going to iterate
-	std::vector<l_grid> & gList;
+	Vcluster_object_array<device_grid> & gList;
 
 	//! Actual iterator
-	grid_key_dx_iterator<dim> a_it;
+	grid_key_dx_iterator_sub<dim> a_it;
+
+	//! margin of the grid iterator
+	size_t m;
 
 	public:
 
@@ -37,18 +39,19 @@ class grid_dist_iterator
 	 * \param gk std::vector of the local grid
 	 *
 	 */
-	grid_dist_iterator(std::vector<l_grid> & gk)
-	:g_c(0),gList(gk)
+	grid_dist_iterator(Vcluster_object_array<device_grid> & gk, size_t m)
+	:g_c(0),gList(gk),m(m)
 	{
-		// Initialize with the current iterator
+		// Initialize the current iterator
 		// with the first grid
 
-		a_it = gList[0].getIterator();
+		a_it.reinitialize(gList[0].getDomainIterator());
 	}
 
 	// Destructor
 	~grid_dist_iterator()
-	{}
+	{
+	}
 
 	/*! \brief Get the next element
 	 *
@@ -56,13 +59,13 @@ class grid_dist_iterator
 	 *
 	 */
 
-	grid_key_dx_iterator<dim> operator++()
+	grid_dist_iterator<dim,device_grid> operator++()
 	{
-		a_it++;
+		++a_it;
 
 		// check if a_it is at the end
 
-		if (a_it.isEnd() == false)
+		if (a_it.isNext() == true)
 			return *this;
 		else
 		{
@@ -72,11 +75,8 @@ class grid_dist_iterator
 
 			// get the next grid iterator
 
-			a_it = a_it = gList[g_c].getIterator();
-
-			// increment to a valid point
-
-			a_it++;
+			if (g_c < gList.size())
+				a_it = gList[g_c].getDomainIterator();
 		}
 
 		return *this;
@@ -88,12 +88,14 @@ class grid_dist_iterator
 	 *
 	 */
 
-	bool isEnd()
+	bool isNext()
 	{
 		// If there are no other grid stop
 
 		if (g_c >= gList.size())
-			return true;
+			return false;
+
+		return true;
 	}
 
 	/*! \brief Get the actual key
@@ -103,9 +105,9 @@ class grid_dist_iterator
 	 */
 	grid_dist_key_dx<dim> get()
 	{
-		return a_it;
+		return grid_dist_key_dx<dim>(g_c,a_it.get());
 	}
 };
 
 
-#endif /* GRID_DIST_ID_ITERATOR_HPP_ */
+#endif /* GRID_DIST_ID_ITERATOR_SUB_HPP_ */
diff --git a/src/Grid/grid_dist_id_iterator_margin.hpp b/src/Grid/grid_dist_id_iterator_margin.hpp
deleted file mode 100644
index 52192dd954d0a29cc0bbbba9a812e62e8aef9c91..0000000000000000000000000000000000000000
--- a/src/Grid/grid_dist_id_iterator_margin.hpp
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * grid_dist_id_iterator_sub.hpp
- *
- *  Created on: Feb 4, 2015
- *      Author: Pietro Incardona
- */
-
-#ifndef GRID_DIST_ID_ITERATOR_SUB_HPP_
-#define GRID_DIST_ID_ITERATOR_SUB_HPP_
-
-#include "VCluster.hpp"
-
-/*! \brief Distributed grid iterator
- *
- * Iterator across the local element of the distributed grid
- *
- */
-
-template<unsigned int dim, typename device_grid>
-class grid_dist_iterator_margin
-{
-	//! grid list counter
-	size_t g_c;
-
-	//! List of the grids we are going to iterate
-	Vcluster_object_array<device_grid> & gList;
-
-	//! Actual iterator
-	grid_key_dx_iterator_sub<dim> * a_it;
-
-	//! margin of the grid iterator
-	size_t m;
-
-	public:
-
-	/*! \brief Constructor of the distributed grid
-	 *
-	 * \param gk std::vector of the local grid
-	 *
-	 */
-	grid_dist_iterator_margin(Vcluster_object_array<device_grid> & gk, size_t m)
-	:g_c(0),gList(gk),m(m)
-	{
-		// Initialize the current iterator
-		// with the first grid
-
-		a_it = new grid_key_dx_iterator_sub<dim>(gList[0].getSubIterator(m));
-	}
-
-	// Destructor
-	~grid_dist_iterator_margin()
-	{
-		// delete the grid iterator
-
-		delete a_it;
-	}
-
-	/*! \brief Get the next element
-	 *
-	 * \return the next grid_key
-	 *
-	 */
-
-	grid_key_dx_iterator<dim> operator++()
-	{
-		a_it++;
-
-		// check if a_it is at the end
-
-		if (a_it.isEnd() == false)
-			return *this;
-		else
-		{
-			// switch to the new grid
-
-			g_c++;
-
-			// get the next grid iterator
-
-			a_it = a_it = gList[g_c].getIterator();
-
-			// increment to a valid point
-
-			a_it++;
-		}
-
-		return *this;
-	}
-
-	/*! \brief Check if there is the next element
-	 *
-	 * \return true if there is the next, false otherwise
-	 *
-	 */
-
-	bool isEnd()
-	{
-		// If there are no other grid stop
-
-		if (g_c >= gList.size())
-			return true;
-	}
-
-	/*! \brief Get the actual key
-	 *
-	 * \return the actual key
-	 *
-	 */
-	grid_dist_key_dx<dim> get()
-	{
-		return a_it;
-	}
-};
-
-
-#endif /* GRID_DIST_ID_ITERATOR_SUB_HPP_ */
diff --git a/src/Grid/grid_dist_id_unit_test.hpp b/src/Grid/grid_dist_id_unit_test.hpp
index e4ca239343054c9a97b67c9a9313cc24cd4b91b2..d4bc120890122230e9f49c2bb655cae738104665 100644
--- a/src/Grid/grid_dist_id_unit_test.hpp
+++ b/src/Grid/grid_dist_id_unit_test.hpp
@@ -30,6 +30,9 @@ template<typename iterator> void jacobi_iteration(iterator g_it, grid_dist_id<2,
 
 BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
 {
+	// Initialize the global VCluster
+	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);
+
 	// grid size
 	size_t sz[2] = {1024,1024};
 
@@ -37,13 +40,22 @@ BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
 
 	grid_dist_id<2, scalar<float>, CartDecomposition<2,size_t>> g_dist(sz);
 
-	// Create the grid on memory
+	// get the domain iterator
 
-	g_dist.Create();
+	size_t count = 0;
+
+	auto dom = g_dist.getDomainIterator();
 
-	// get the Bulk iterator
+	while (dom.isNext())
+	{
+		auto key = dom.get();
+
+		count++;
+
+		++dom;
+	}
 
-	auto bulk = g_dist.getBulkIterator(2);
+	BOOST_REQUIRE_EQUAL(count,1024*1024);
 
 /*	auto g_it = g_dist.getIteratorBulk();
 
diff --git a/src/Grid/grid_dist_key.hpp b/src/Grid/grid_dist_key.hpp
index 3a45a1e198b63220d80a302fc8530814bb80db24..ae8c7cd263318632afbb91ddb64f297375e2ef9a 100644
--- a/src/Grid/grid_dist_key.hpp
+++ b/src/Grid/grid_dist_key.hpp
@@ -16,7 +16,14 @@ class grid_dist_key_dx
 
 	//! Local grid iterator
 
-	grid_key_dx_iterator<dim> a_it;
+	grid_key_dx<dim> key;
+
+public:
+
+	grid_dist_key_dx(int g_c, grid_key_dx<dim> key)
+	:g_c(g_c),key(key)
+	{
+	}
 };
 
 #endif