Patches provided by Joel Baker in PR 22280, verified by myself.
into separate modules to avoid namespace clashes.