More refactoring and cleanup