Cherry-picked changes from master, bfbc5ef..e29fc64