diff --git a/src/engine.c b/src/engine.c
index 519f979928ccf22da7ec8052cfad71eb8fa17284..a6291a0c87b45ceb4ff8db773277ddb9164d5aad 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -974,7 +974,9 @@ void engine_maketasks ( struct engine *e ) {
     /* Split the tasks. */
     scheduler_splittasks( sched );
     
-    /* Allocate the list of cell-task links. */
+    /* Allocate the list of cell-task links. The maximum number of links
+       is the number of cells (s->tot_cells) times the number of neighbours (27)
+       times the number of interaction types (2, density and force). */
     if ( e->links != NULL )
         free( e->links );
     if ( ( e->links = malloc( sizeof(struct link) * s->tot_cells * 27 * 2 ) ) == NULL )
@@ -1077,7 +1079,7 @@ void engine_maketasks ( struct engine *e ) {
         /* Skip? */
         if ( t->skip )
             continue;
-        
+            
         /* Self-interaction? */
         if ( t->type == task_type_self && t->subtype == task_subtype_density ) {
             scheduler_addunlock( sched , t , t->ci->super->ghost );
diff --git a/src/space.c b/src/space.c
index 5969d788eea28a22a4c38a8579722aeaa9ef3387..282372d211d91115ded39e85fa853026b3d8589f 100644
--- a/src/space.c
+++ b/src/space.c
@@ -192,6 +192,10 @@ void space_regrid ( struct space *s , double cell_max ) {
     for ( k = 0 ; k < 3 ; k++ )
         cdim[k] = floor( s->dim[k] / fmax( h_max*kernel_gamma*space_stretch , cell_max ) );
         
+    /* Check if we have enough cells for periodicity. */
+    if ( s->periodic && (cdim[0] < 3 || cdim[1] < 3 || cdim[2] < 3) )
+        error( "Must have at least 3 cells in each spatial dimension when periodicity is switched on." );
+        
     /* In MPI-Land, we're not allowed to change the top-level cell size. */
     #ifdef WITH_MPI
         if ( cdim[0] < s->cdim[0] || cdim[1] < s->cdim[1] || cdim[2] < s->cdim[2] )