Simplify and add checks for DFS and BFS