Rearranged code a little bit