Converted double foreach to 1 for