optimized some loops