rewrite error handling