rearranged code a little