rearranging code to work better