Some ambitious refactoring