initial commit of working R source file. Works okay, but still needs work