Extensive refactoring.