Revised R code