improving custom pathfinding script for better performance and distance handling