Clean up and minor refactoring