Added AJAX functionality and fixed error handling.