rearrange the loop