Cherry-picked changes from master, 7e7fb39..bfbc5ef