Backport2886and7

This backports #2886 and #2887 into 1.7. Fixes #2889.