Updated method to use a for-each loop instead of an iterator.