Some optimisations and tidying