Cherry-picked changes from master, 39bc797..7876cdd