Computation optimisation

This provides big speedups for computations involving long graphs, fixing #963.