Fully working set of functions. Initial tests completed. End of main.r now has test run with increasing values of J.