Fix bugs.
Now passing all test cases.