Some design refactoring and code cleanup.