Implemented some unit tests and fixed a few bugs in the process.