rearrange some code