Some more cleanup and refactoring