Refactored the processing of a directory and made a function for it.