Added unit tests, and fixed some minor bugs.