backport of #692