diff --git a/src/cal_tools/agipdutils.py b/src/cal_tools/agipdutils.py
index 280f07324f8076855a5e3b76eb59a38f2c5c9ab7..a5d7cb628c3557aa75d6352157fb5592e6a0d9ae 100644
--- a/src/cal_tools/agipdutils.py
+++ b/src/cal_tools/agipdutils.py
@@ -135,6 +135,16 @@ def baseline_correct_via_stripe(d, g, m, frac_high_med):
     :param m: bad pixel mask
     """
 
+    # Restrict the mask to those bad pixels obtained from darks.
+    # This has ben introduced since the BadPixelsFF constants can add
+    # additional masking that causes the baseline correction to fail due
+    # to the abort conditions len(idx) < 3 below.
+    # (see calibration/planning#96)
+    m = m & (BadPixels.OFFSET_OUT_OF_THRESHOLD |
+             BadPixels.NOISE_OUT_OF_THRESHOLD |
+             BadPixels.OFFSET_NOISE_EVAL_ERROR |
+             BadPixels.NO_DARK_DATA)
+
     dd = copy.copy(d)
     dd[g != 0] = np.nan  # only high gain data
     dd[m != 0] = np.nan  # only good pixels