Refactoring the code, used pathname to iterate over every file in the directory