Major refactoring, ready for new features