Pr 1236 rebased

This is a follow up to #1236.  It's @michihuber's work plus the start of fixes I've applied based on mine and @JonRowe's feedback on that PR.  This will supercede that PR.

There's a bit more to do.