cherry-pick 4342 and 4344 merged manually