refactored without using glob module