Added unit tests, fixed bug