Applied Pete Henry's fix to R code