minor optimisations and cleaning up