Refactoring ajax poll logic to be more flexible