diff --git a/src/engine.c b/src/engine.c
index ba89349d3fae95e7ca93b91e31de45d43f41845f..bb519e73c8d76b68c91e3987c9524a82f074ef80 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -843,9 +843,12 @@ int engine_exchange_strays ( struct engine *e , int offset , int *ind , int N )
     
     /* Put the parts into the corresponding proxies. */
     for ( k = 0 ; k < N ; k++ ) {
-        pid = e->proxy_ind[ e->s->cells[ ind[k] ].nodeID ];
+        int node_id = e->s->cells[ ind[k] ].nodeID;
+        if (node_id < 0 || node_id >= e->nr_nodes)
+          error("Bad node ID %i.", node_id);
+        pid = e->proxy_ind[ node_id ];
         if ( pid < 0 )
-            error( "Do not have a proxy for the requested nodeID." );
+            error( "Do not have a proxy for the requested nodeID %i.", node_id );
         proxy_parts_load( &e->proxies[pid] , &s->parts[offset + k] , &s->xparts[offset + k] , 1 );
         }
     
diff --git a/src/space.c b/src/space.c
index a5969d3602e1e70b5723ac8e162598620c0b408a..3f7f0ec8c519f45c4b004770b376278d1d307c9f 100644
--- a/src/space.c
+++ b/src/space.c
@@ -336,6 +336,8 @@ void space_rebuild ( struct space *s , double cell_max ) {
             else if ( p->x[j] >= dim[j] )
                 p->x[j] -= dim[j];
         ind[k] = cell_getid( cdim , p->x[0]*ih[0] , p->x[1]*ih[1] , p->x[2]*ih[2] );
+        if (ind[k] < 0 || ind[k] >= s->nr_cells)
+          error("Bad cell id %i.", ind[k]);
         atomic_inc( &cells[ ind[k] ].count );
         }
     // message( "getting particle indices took %.3f ms." , (double)(getticks() - tic) / CPU_TPS * 1000 );
@@ -344,24 +346,25 @@ void space_rebuild ( struct space *s , double cell_max ) {
     #ifdef WITH_MPI
         /* Move non-local parts to the end of the list. */
         int nodeID = s->e->nodeID;
+        int nr_local_parts = s->nr_parts;
         for ( k = 0 ; k < nr_parts ; k++ )
             if ( cells[ ind[k] ].nodeID != nodeID ) {
                 cells[ ind[k] ].count -= 1;
-                nr_parts -= 1;
+                nr_local_parts -= 1;
                 struct part tp = parts[k];
-                parts[k] = parts[ nr_parts ];
-                parts[ nr_parts ] = tp;
+                parts[k] = parts[ nr_local_parts ];
+                parts[ nr_local_parts ] = tp;
                 struct xpart txp = xparts[k];
-                xparts[k] = xparts[ nr_parts ];
-                xparts[ nr_parts ] = txp;
+                xparts[k] = xparts[ nr_local_parts ];
+                xparts[ nr_local_parts ] = txp;
                 int t = ind[k];
-                ind[k] = ind[ nr_parts ];
-                ind[ nr_parts ] = t;
+                ind[k] = ind[ nr_local_parts ];
+                ind[ nr_local_parts ] = t;
                 }
                 
         /* Exchange the strays, note that this potentially re-allocates
            the parts arrays. */
-        s->nr_parts = nr_parts + engine_exchange_strays( s->e , nr_parts , &ind[nr_parts] , s->nr_parts - nr_parts );
+        s->nr_parts = nr_local_parts + engine_exchange_strays( s->e , nr_local_parts , &ind[nr_parts] , nr_parts - nr_local_parts );
         parts = s->parts;
         xparts = s->xparts;