Changes directory logic to use a directory iterator instead of selecting all directories for better performance.