Cleanup and a bit of refactoring.