fixed dfs algorithm to contine properly