fixed typo in R script, reordered funcs, reorded script steps