More general clean-up and refactoring