refactored mazelogic to be more efficient