Another bug fix based on $.each