lots of changes to r script