Clear and refactor code