Implement (most of) #2211

See #2211 for details