extensive refactoring