Added some unit tests. Removed some bugs.