rearranged code a bit