fix bug and added unit tests