refact to use only iterators and not loops