Further refactoring and cleanup.