backport #1313 which is addition to #1300