nearly done pathfinding code