rearranged some code