Cherry-picked changes from master, ff83d3b..39bc797