reorganize code using  deferred objects to eliminate nested call backs.