factoring out graph building a little bit