Some more optimizations, removed Array.forEach and Math.sqrt