diff --git a/main.py b/main.py
index 15ed4ca95e8e90b481fd63c054a166eaf489bafc..0326b51e43859c9b60487159297e6e4e11193af6 100644
--- a/main.py
+++ b/main.py
@@ -49,9 +49,9 @@ while True:
         coord, max_spot_size = ion_positions(image)
         if len(coord) > 0:
             roundness = ion_roundness(image, coord[0][:2])
-        new_img = compensate_gradient(image)
-        max_int, num_pix = max_ion_intensity(image, coord)
-        coord = detect_wrong_isotopes(image, coord, max_int)
+        new_img = compensate_gradient(image, coord)
+        max_int, num_pix = max_ion_intensity(new_img, coord)
+        coord = detect_wrong_isotopes(new_img, coord, max_int)
         LINEARITY = ion_linearity(coord)
         if len(coord) > 1 and dist_factor is not None:
             coord = dark_ions(image, coord)
diff --git a/tools.py b/tools.py
index 963e793c48bda2ed461cbba25081fc39948f8d0c..bd87614962ee730736b61ffdca568ce1366ded18 100644
--- a/tools.py
+++ b/tools.py
@@ -218,24 +218,12 @@ def ion_positions(image):
     Finally return the coordinates together with the maximum ion width.
     """
     coordinates, max_width = _max_coord(image, width=True) # measure local max
-    # delete local maxima that are not in the same axis as the brightest spot.
-    try:
-        h_axis = coordinates[0, 0]
-        coordinates = coordinates[np.logical_and(coordinates[:, 0] < h_axis + 15,
-        coordinates[:, 0] > h_axis - 15)]
-    except IndexError:
-        pass
-
     coordinates += np.flip(settings.REGION[0]) # change reference
     coordinates = coordinates[coordinates[:, 1].argsort()] # order
     # add 3rd column indicating the ion's state (bright, dark, wrong isotope)
     coordinates = np.concatenate((coordinates, np.zeros((len(coordinates[:, 1]), 1))), axis=1)
 
-    if len(coordinates) > 25:
-        coordinates = np.empty((0, 3))
-        print('We have an ion cloud')
-
-    elif len(coordinates) == 0:
+    if len(coordinates) == 0:
         coordinates = np.empty((0, 3))
 
     return coordinates.astype(int), max_width
@@ -291,21 +279,18 @@ def ion_linearity(points):
     return linearity
 
 
-def compensate_gradient(image):
+def compensate_gradient(image, coord):
     """
     Measure the intensity gradient of the ions along the x axis and correct it.
     Ignore any basckground intensity gradient.
     """
-    im_slope = _measure_gradient(image)
-
-    background_slope = _measure_gradient(image[np.r_[0:200, image.shape[0]-200:image.shape[0]]])
 
-    slope = im_slope - background_slope
+    slope = _measure_gradient(image, coord)
 
     for i in range(image.shape[1]):
-        image[:, i] = image[:, i] - i*slope
+        image[:, i] = np.clip(image[:, i] - i*slope, 0, None)
 
-    return image
+    return image.astype(np.uint16)
 
 
 def max_ion_intensity(image, centers):
@@ -631,16 +616,19 @@ def _smooth(x, window_len=11, window='hanning'):
     return y
 
 
-def _measure_gradient(image):
+def _measure_gradient(image, coord):
     """
-    Fits the image intensity into a polynomial function of degree one and returns
+    Fits the ion intensity into a polynomial function of degree one and returns
     the value of the slope.
     """
-    col_int = np.sum(image, axis=0)
-    x = np.arange(image.shape[1], step=1)
-    m, _ = np.polyfit(x, col_int, deg=1)
+    y = []
+    x = []
+    for pos in coord:
+        y.append(np.sum(image[pos[0] - 30 : pos[0] + 30, pos[1]], axis=0))
+        x.append(pos[1])
+    m, _ = np.polyfit(x, y, deg=1)
 
-    return m/image.shape[0]
+    return m/60
 
 
 def _ion_intensity(image, center):