fixing code and add unit tests