changed to use foreach loop. x is the index, not the current object!