partial cleanup and refactoring