Fixes the infinite loop issue with pathfinding.