Optimized loop over Z