Changing a for with a foreach