Rebase and fix of #8437