Also added cycle checking to BFS