Rearrange some code