Refactoring, rearranging and cleanup