refactor the error handling