Large optimisations and fixes