Working version of the R script