Fixed the special case bugs. Added several new test cases.