Some fixes and optimisations