backport of #3495