Backport changes from mainline