Refactor to use jQuery.when()