refactor ajax handler