added unit tests and fixed bug