refactor code r3