This is all code from #261 ; no changes were made beyond that in this branch (but needed those changes to test)