Backport fix for #1066