Fix #110. One line of code to fix the bug and about 100 lines of unit tests.