A couple of bug fixes: all test cases pass