beta version, R code almost finished