Added unit test, small bug fixes