more code cleanup and refactoring