Backport fixes from trunk.