Major refactoring. Use q.defer.