adding unit test

Adding unit tests and fixing logic error