Some optimisation, mostly by making local caches of values.