Extracted directory listing functionality to its own function