Major refactorings to provide better support for new features in the future