Updated to handle empty directories.