Fully resolved issue #709.

The previous fix worked only for the specific case mentioned in the
bug report, this version works for all cases.
Added additional test case to test issue 709.
Revert "Fixes: #709, off by one error"

This reverts commit 048151a80fcb0fef7808b63581c7c58f46eb8f73.
Merge remote-tracking branch 'mudhairless/709-again'
tests: Add test case from #709 report