refactor dir detection