Using $.each instead of for loop for better compatibility with IE