New version with better for loops