Backport fix for #977