Performance optimisations and cleanup