Finalized comments for overall R script and 2 subfunctions.