Backport of #659
Backport of #659