refactor ajax code