Improve API and request error handling