Cherry-picked changes from master, 4020e23..ff83d3b