Native javascript loops

- Replaced $.each loops with faster native "for" ones.