Added unit tests + fixed some bugs