rearrange logic