Optimized some loops.