Refactored maze solver