Moved some of the path finding code into a generator for readability and efficiency