fix error handling and add tests