diff --git a/Trajectories/cost_function.py b/Trajectories/cost_function.py
index 0c09635cb89170e81975a4e3b939e1f9abc7685b..192e7719e16acc0efa2454bfe8def1dac4501410 100755
--- a/Trajectories/cost_function.py
+++ b/Trajectories/cost_function.py
@@ -102,6 +102,11 @@ def node_to_prop(node_list, e_overestim, SHPC, orientation):
 
     return properties
 
+def search_beg(inst_eddies, max_delta):
+    return min(max_delta + 1, len(inst_eddies))
+
+def search_end(inst_eddies, max_delta):
+    return max(len(inst_eddies) - max_delta - 1, 0)
 
 t0 = time.perf_counter()
 timings = open("timings_cost.txt", "w")
@@ -174,7 +179,7 @@ print("Iterating on vertices...")
 for n in g.vertices():
     if n.in_degree() != 0:
         # Define properties for beginning of the segment:
-        ip_beg = min(n_days_avg, len(g.vp.inst_eddies[n]))
+        ip_beg = search_beg(g.vp.inst_eddies[n], n_days_avg - 1)
         properties = node_to_prop(
             g.vp.inst_eddies[n][:ip_beg],
             g.gp.e_overestim,
@@ -194,7 +199,7 @@ for n in g.vertices():
         if ip_beg < len_seg:
             # We have to read more from the shapefiles and redefine
             # properties.
-            ip_end = max(len_seg - n_days_avg, 0)
+            ip_end = search_end(g.vp.inst_eddies[n], n_days_avg - 1)
 
             if ip_beg <= ip_end:
                 # We cannot use part of properties from the beginning