Contributed by Thomas Marquardt. (cherry picked from commit 749fff577ed9afb4ef8a54b8948f74be083cc620)