Extensive refactoring. Jea