replace for by foreach loop whenever possible