Porting upstream's 378ffdebb944a2e501c6c3bbc17e21e529bcc14c