Added a fail callback to the deferred
Uses $.each for two more loops