Clean and refactor code