R refactor