Rewrite library using $.Deferred