QD-50 : merged in two patches/pull requests, made more changes, fixed the tests

https://bugs.indeed.com/browse/QD-50

  rebased onto latest from upstream, reapplied same patches, and my same fixes, and fixed the tests.