Refactors listFiles function to use closure