Refactored AJAX function

Refactored AJAX function