minor bug fixing and not that minor optimisations