Some cleanup and refactoring