Performance optimisations and bugfixes