Further cleaning and refactoring