More refactoring and code organization