rearranging and cleaning code