cleaned up R code