broke up R functions into several perhaps reuseable pieces