backport more changes from upstream patch