better error handling and tests for errors