refactor and make the graph drawing code run less