refactor to be smarter when opening a directory