Added more unit tests and fixed a few bugs in the process.