fixing bugs found in the code

I added test cases and fixed some bugs found in the code review.