Backport remaining changes from commit 7e09ef0