Code cleaning and refactor