added jquery deferred and refactoring