Rearranged some code