Various minor changes and convert apply-samples.R to Rmd