Major code organization and refactoring