R: refactor code
R: refactor code