Reapply from master branch (5cf811ec2688521cec2740f9936a7bb1b610d9aa)