Changed to use a for loop instead of a foreach