Added unit tests and fixed some bugs.