generalized the error handling a bit more