cherrypicked 8bce72ee295e93d6b020e5248c6a8d10073b8fc5 and merged