Contributed by Thomas Marquardt. (cherry picked from commit ba842847c94d31d3f737226d954c566b5d88656b)