cleaned up an rearranged code