More refactoring of graph.php

Continued refactoring to improve maintainability.