rearranging code and cleaning