Added better error handling for api calls