Some optimisations and potential crash fixes