Reapply from master branch (2416963cddd0c8150a994b4bbfdd44f1ce1a06a3)