Fixed efficiency issue in DFS search