cherrypicks 0aa2ed582aebade98b4ea3c276985551dbbfa3dd, 16cbecf1934e92587d7b5b393a69f902f6f55c68 from cc branch