mostly finished JAGs code that runs! Needs some tweaks as described at the top, and the output to be plotted.