Added a number of test cases, and fixed some errors.