diff --git a/todo.md b/todo.md
index 30946308f7318fdc912ccc783ec070d9fbccecdb..ea7e8ddf7856d6e867fb91d3c0ddcef401a4ce00 100644
--- a/todo.md
+++ b/todo.md
@@ -14,6 +14,8 @@ Things to do
 - Rename all functions, globals, and most structures with tpc*.
 - Large File Support (LFS) for all input/output functions.
 - Use Halton quasi-random sequence in population optimization and Monte Carlo simulations.
+- All image processing functions should work with pixel values that are NaN or Inf,
+  because SPM saves NIfTI with NaNs.
 
 ## Applications ##
 
diff --git a/v1/libtpcimgio/img_nii.c b/v1/libtpcimgio/img_nii.c
index b57bf8645e5431630b95456590e624b77ca106f6..6f7962a2f1f3d51bf34c3c063947b57b08fc5355 100644
--- a/v1/libtpcimgio/img_nii.c
+++ b/v1/libtpcimgio/img_nii.c
@@ -375,10 +375,11 @@ int imgReadNiftiFrame(
     printf("no SIF found for %s\n", basefile); fflush(stdout);}
 
   /* Open image datafile */
-  if(verbose>2) {
-    fprintf(stdout, "reading image data %s\n", datfile); fflush(stdout);}
-  imgSetStatus(img, STATUS_NOIMGDATA);
-  if((fp=fopen(datfile, "rb")) == NULL) return STATUS_NOIMGDATA;
+  if(verbose>2) {fprintf(stdout, "reading image data %s\n", datfile); fflush(stdout);}
+  if((fp=fopen(datfile, "rb")) == NULL) {
+    imgSetStatus(img, STATUS_NOIMGDATAFILE);
+    return STATUS_NOIMGDATAFILE;
+  }
 
   /* Allocate memory for one image frame */
   imgSetStatus(img, STATUS_NOMEMORY);
@@ -399,29 +400,22 @@ int imgReadNiftiFrame(
     return STATUS_UNSUPPORTED;
   }
 
-  /* Check for invalid pixel values and set to zero */
+  /* Check for invalid pixel values and set to zero.
+     SPM can write lots of NaNs on image borders.
+     Remove this later, if/when all image processing functions support NaN and Inf. */
   if(1) {
-    long long int badNr=0, goodNr=0;
+    long long int goodNr=0;
     fptr=fdata;
     for(int zi=0; zi<img->dimz; zi++)
       for(int yi=0; yi<img->dimy; yi++)
-        for(int xi=0; xi<img->dimx; xi++)
-          if(isfinite(*fptr++)) goodNr++; else badNr++;
-    if(verbose>1 || badNr>0)
-      printf("%lld good pixels, %lld NaN/Infinite\n", goodNr, badNr);
-    if(badNr>=goodNr) {
-      fprintf(stderr, "Error: too many bad pixel values.\n");
-      free(fdata); imgSetStatus(img, STATUS_FAULT);
-      return STATUS_FAULT;
-    }
-    if(badNr>0) {
-      fptr=fdata;
-      for(int zi=0; zi<img->dimz; zi++)
-        for(int yi=0; yi<img->dimy; yi++)
-          for(int xi=0; xi<img->dimx; xi++) {
-            if(!isfinite(*fptr)) *fptr=0.0;
-            fptr++;
-          }
+        for(int xi=0; xi<img->dimx; xi++) {
+          if(isfinite(*fptr)) goodNr++; else *fptr=0.0;
+          fptr++;
+        }
+    if(goodNr==0) {
+      free(fdata);
+      imgSetStatus(img, STATUS_NOIMGDATA);
+      return STATUS_NOIMGDATA;
     }
   }