factor out the graph code