Implemented stolen A* function.