Tidied up the R code