improve dfs aglorithm