improved r code