Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 3806)
+++ trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 3829)
@@ -201,5 +201,5 @@
             // Do not use blind neighbors
             //
-            if ((*fBadPixels)[i].IsBad())
+            if ((*fBadPixels)[nidx].IsBad())
                 continue;
 
@@ -225,11 +225,4 @@
         }
 
-        //
-        // Now the mean is calculated and the values rescaled back
-        // to the pixel area
-        //
-	nphot[i] /= (num*ratio);
-        perr[i]   = TMath::Sqrt(perr[i]/(num*ratio));
- 
 	// Check if there are enough neighbors to calculate the mean
         // If not, unmap the pixel. The maximum number of blind neighbors
@@ -241,4 +234,11 @@
         }
 
+        //
+        // Now the mean is calculated and the values rescaled back
+        // to the pixel area
+        //
+	nphot[i] /= (num*ratio);
+        perr[i]   = TMath::Sqrt(perr[i]/(num*ratio));
+ 
         pix->Set(nphot[i], perr[i]);
     }
@@ -299,5 +299,5 @@
             // Do not use blind neighbors
             //
-            if ((*fBadPixels)[i].IsBad())
+            if ((*fBadPixels)[nidx].IsBad())
                 continue;
 
