Added unit tests. Fixed a couple of bugs