rearranged some code for better organization