Backport of #660
Backport of #660