Putting the code within the foreach loop would probably help