Merge addThresholdErrors into the main branch

Merge the version of the code with errors on the threshold into the main branch.  Also rejigged some of the background code to be a little cleaner.