lots of optimisations and fixes