Extensive refactoring