More refactoring and code cleanup.