Backport of #1845.
Backport of #1845.