rearranged the code a bit