Finished optimisations and bug fixes