Refactored error, auth and request/response handling to be more robust