More cleanup and refactoring