Refactored function that fetches the list of files