Backport of 86f4459f9e from master
86f4459f9e
Tests by Tim Graham, report from jeroen.pulles@redslider.net. Backport of 1ed77e7782 from master
1ed77e7782