Updated version of R script