added unit tests and support for errors