Tried to make internal error handling more flexible