Bugfix

Bugfix resolved problems with the special cases. Special cases were conceptually clarified in the comment, and the code dealing with them was rewritten. See in-line comments for a description of the special cases and how they were fixed.