Final refactoring, ready for code review