Replacement for @raoulb pr#914

See #914

ToDo:
- [x] tests
- [x] final rebase
Is this necessary? Perhaps just "pass" if it's not.