Contributed by Wilfred Spiegelenburg. (cherry picked from commit a97f7776bd05f957a998b4883141855cad5afb31)