Lovingly cherry picked by hand from an accidental push of commit b51b933b and 3c4d5c5e.
Fixes #895 (closed) #1009 (closed)