Backport #1050 and #1051