split out directory iterator to a new function