Backport of recent changes.