Some cleanup, minor refactoring and restructuring