major refactoring and cleanup