Hassan's fix in DFS algorithm