AJAX handler to refactor