Thanks Carl Meyer and Tim Graham for the reviews and to all involved in the discussion. Backport of 18c0aaa912 from master
18c0aaa912
Thanks to Russell Keith-Magee for mentoring this Google Summer of Code 2014 project and everyone else who helped with the patch!