better level traverse algorithm

with better control flow, the run time dropped from 40ms+ to ~16ms