wrote iterator function, may be memory hog