Rearranging and minor refactoring