Code cleaning and refactoring