Reordered step3 loops for better cache locality