Backport 1821

backport of part of #1821