refactored code, added function to iterate on directory listing results