back porting 841a8c363e1073f0509586ba917bf61d9af171e6 from master