refactoring DFS