diff --git a/src/main/java/org/orekit/rugged/api/Rugged.java b/src/main/java/org/orekit/rugged/api/Rugged.java
index 2f5a169564f60284975346c0c865ccedb3ddc314..a0fdab4650f6ef680dfcde93619a3425d686b371 100644
--- a/src/main/java/org/orekit/rugged/api/Rugged.java
+++ b/src/main/java/org/orekit/rugged/api/Rugged.java
@@ -814,10 +814,7 @@ public class Rugged {
                     }
 
                     // Check if the pixel is inside the sensor (with a margin) OR if the inverse location was impossible (null result)
-                    if (sensorPixelGrid[uIndex][vIndex] != null &&
-                        (sensorPixelGrid[uIndex][vIndex].getPixelNumber() < -INVLOC_MARGIN ||
-                         sensorPixelGrid[uIndex][vIndex].getPixelNumber() > (INVLOC_MARGIN + sensor.getNbPixels() - 1)) ||
-                        sensorPixelGrid[uIndex][vIndex] == null) {
+                    if (!pixelIsInside(sensorPixelGrid[uIndex][vIndex], sensor)) {
                         // In order for the dump to end nicely
                         DumpManager.endNicely();
                         // Impossible to find the point in the given min line
@@ -839,6 +836,15 @@ public class Rugged {
         return sensorPixelGrid;
     }
 
+    /** Check if pixel is inside the sensor with a margin.
+     * @param pixel pixel to check (may be null if not found)
+     * @param sensor the line sensor
+     * @return true if the pixel is inside the sensor
+     */
+    private boolean pixelIsInside(final SensorPixel pixel, final LineSensor sensor) {
+        return pixel != null && pixel.getPixelNumber() >= -INVLOC_MARGIN && pixel.getPixelNumber() < INVLOC_MARGIN + sensor.getNbPixels() - 1;
+    }
+
     /** Computation, for the sensor pixels grid, of the direct location WITH atmospheric refraction.
      * (full computation)
      * @param pixelGrid the pixel grid
diff --git a/src/test/java/org/orekit/rugged/refraction/AtmosphericRefractionTest.java b/src/test/java/org/orekit/rugged/refraction/AtmosphericRefractionTest.java
index ab6bf69e7559d8eb023457a99160e83ef2308687..9a0dd896aed15ef97fd80ab21407a5b011abc370 100644
--- a/src/test/java/org/orekit/rugged/refraction/AtmosphericRefractionTest.java
+++ b/src/test/java/org/orekit/rugged/refraction/AtmosphericRefractionTest.java
@@ -120,9 +120,29 @@ public class AtmosphericRefractionTest {
         GeodeticPoint dummyGP = new GeodeticPoint(dummyLat, dummyLon, 0.);
         try {
             ruggedWith.inverseLocation(sensorName, dummyGP, minLine, maxLine);
+            Assert.fail("an exeption should have been thrown");
         } catch (RuggedException re) {
             Assert.assertEquals(RuggedMessages.INVALID_RANGE_FOR_LINES, re.getSpecifier());
         }
+
+        try {
+            ruggedWith.inverseLocation(sensorName,
+                                       gpWithAtmosphericRefractionCorrection[0],
+                                       210, maxLine);
+            Assert.fail("an exeption should have been thrown");
+        } catch (RuggedException re) {
+            Assert.assertEquals(RuggedMessages.INVALID_RANGE_FOR_LINES, re.getSpecifier());
+        }
+
+        try {
+            ruggedWith.inverseLocation(sensorName,
+                                       gpWithAtmosphericRefractionCorrection[0],
+                                       minLine, 190);
+            Assert.fail("an exeption should have been thrown");
+        } catch (RuggedException re) {
+            Assert.assertEquals(RuggedMessages.INVALID_RANGE_FOR_LINES, re.getSpecifier());
+        }
+
     }
 
     private RuggedBuilder initRuggedForAtmosphericTests(final int dimension, final String sensorName) throws URISyntaxException {