optimization and error handling in AJAX calls