Cherry-pick some improvements from @jesyspa's 118d435
Cherry-pick more of @jesyspa's improvements from 442121f