More work on refactoring and cleanup