Rearranged lots of code