Stolen as per @parkr's suggestion on #1095 :)