add tests and fix error handling