Tweaked A* algorithm.