refactored buildMaze function