diff --git a/examples/test.c b/examples/test.c
index d928bb55b37456b0a03b922e80a1f2925b421a54..823bb28ceffb08c375f13baccec493dc49bf092a 100644
--- a/examples/test.c
+++ b/examples/test.c
@@ -886,7 +886,7 @@ int main ( int argc , char *argv[] ) {
 
     /* Dump the first few particles. */
     for(k=0; k<10; ++k)
-      printParticle(parts, k);
+      printParticle(parts, k, N);
 
     tic = getticks();
     write_output("output.hdf5", dim, parts, N, periodic);
@@ -969,8 +969,8 @@ int main ( int argc , char *argv[] ) {
         
         /* Dump the first few particles. */
         for(k=0; k<10; ++k)
-          printParticle(parts, k);
-        printParticle( parts , 113531 );
+          printParticle(parts, k, N );
+        printParticle( parts , 113531, N );
     
         /* Get the particle with the lowest h. */
         p = &s.parts[0];
@@ -1053,7 +1053,7 @@ int main ( int argc , char *argv[] ) {
     
     /* Dump the first few particles. */
     for(k=0; k<10; ++k)
-      printParticle(parts, k);
+      printParticle(parts, k, N);
     
     /* Get all the cells of a certain depth. */
     // icount = 1;
diff --git a/src/debug.c b/src/debug.c
index aabeb7aea9501b58eae449e3eb20a00b3e116e59..8da81a5e8206208552a4516f55e6055e78b255a1 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -23,13 +23,24 @@
 
 #include "part.h"
 
-void printParticle ( struct part *parts , long long int id ) {
+
+/**
+ * @brief Looks for the particle with the given id and prints its information to the standard output.
+ * 
+ * @param parts The array of particles.
+ * @param id The id too look for.
+ * @param N The size of the array of particles.
+ *
+ * (Should be used for debugging only as it runs in O(N).)
+ */
+void printParticle ( struct part *parts , long long int id, int N ) {
 
     int i;
 
     /* Look for the particle. */
-    for ( i = 0 ; parts[i].id != id ; i++ );
+    for ( i = 0 ; i < N && parts[i].id != id; i++ );
 
+    if(i < N)
   printf("## Particle[%d]: id=%lld, x=[%.3e,%.3e,%.3e], v=[%.3e,%.3e,%.3e], a=[%.3e,%.3e,%.3e], h=%.3e, h_dt=%.3e, wcount=%.3e, m=%.3e, rho=%.3e, rho_dh=%.3e, div_v=%.3e, u=%.3e, dudt=%.3e, bals=%.3e, POrho2=%.3e, v_sig=%.3e, dt=%.3e\n",
 	 i,
 	 parts[i].id,
@@ -49,5 +60,7 @@ void printParticle ( struct part *parts , long long int id ) {
      parts[i].force.v_sig,
 	 parts[i].dt
 	 );
+    else
+      printf("## Particles[???] id=%lld not found\n", id);
 }
 
diff --git a/src/debug.h b/src/debug.h
index 693c50f6b0f74ddb20d859a02639b22f0040c535..b824edf59930be0be813bfc5e5867020bc52097d 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -20,4 +20,4 @@
 
 
 
-void printParticle(struct part *parts, long long int i);
+void printParticle(struct part *parts, long long int i, int N);