refactoring graph building method