Refactor AJAX and sort method