Refactor maze traversal problem