Used A* algorithm to select which direction to try first