a little code optimisation on building graph