Switched from explicitly-index for loops to jQuery.each