Initial commit of R files, working but slow