Used $.each() instead of for() where it was appropriate