Backport of #538
Backport of #538