Changed loops to for(var i) for better performance.