modified R function