Cherry pick commits to fix background generation
!25 (merged) introduced some issues which had previously been fixed by @qi-chu in branch max_snr_point
There were 4 relevant commits which I've cherry-picked (the others were new features and fixes to those features) The first 3 are direct fixes, the 4th is a small refactors/fixes to debug output.
I've edited a couple of the commits during the cherry pick merge, and made done some refactoring.
Edited by Timothy Davies