refactor logic to use jquery deferreds