Some cleaning and refactoring