Backport of #902 to master