PR #1804 rebased with integrated feedback

That work was intersecting too much with another pending pull request so they met in the middle. This addresses I hope all the many comments left on #1804, and then some. Review by @xeno-by.