Enhanced to handle subdirectories