Refactor ajax query