More support for cache/optimisations and lots of refactors