Refactor maze fn