Backport of #1070