Made the graph class more robust to avoid crashing