fix error handling and some tests