implemented dfs versoin of checking