Added proper error handling for API