Refactoring the AJAX