Rearranged the code a bit