Backport of 7d1b69dbe7f72ac04d2513f0468fe2146231b286 from master
Backport of 104ee2fdae7fcd402396c709c91f5c84de6e4165 from master