rearrange loops