Updated DFS code