added unit tests and fixed a bug