Some cleanup and refactoring.