re-editted the R code again