Added better error messages and handle failed XHR requests properly.