Added more tests and fixed some bugs in the process.