Backport Mikhail's abf7f75 from master