Some bug fixes copied from my local code (which was used to generate the figures in the manuscript submission)