Index: trunk/psModules/src/detrend/pmMaskBadPixels.c
===================================================================
--- trunk/psModules/src/detrend/pmMaskBadPixels.c	(revision 10022)
+++ trunk/psModules/src/detrend/pmMaskBadPixels.c	(revision 10026)
@@ -165,9 +165,17 @@
         }
 
-        float target = histo->nums->data.F32[0] * powf(10.0, thresh); // Level to which histogram must fall
-        int index;
-        for (index = 0; histo->nums->data.F32[index] > target && index < histo->nums->n; index++)
-            ;
-        limit = histo->bounds->data.F32[index];
+        // Find the mode.  Since this is a Poisson distribution (more or less), this should also be the mean
+        // and variance.
+        int max = 0;                    // Index of the mode
+        for (int i = 0; i < histo->nums->n; i++) {
+            if (histo->nums->data.F32[i] > histo->nums->data.F32[max]) {
+                max = i;
+            }
+        }
+
+        // Since the mode is most likely zero, we add one to get something realistic.  Then "thresh" is
+        // negative, so we subtract instead of add.
+        limit = max + 1.0 - thresh * sqrtf((float)max + 1.0);
+
     } else {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
