Added more robust error handling in ajax calls