Changed the for loop to use $.each instead