Fixed function to account for boundary max lags