Heavy refactoring. Still incomplete