Cherry-picked changes from master, e29fc64..4670c95