chanages to the R script
Merge with Ming's changes