refactoring to DFS