added several kinds of error handling