fixed bugs in flatten_dir