Fix null pointers and some other errors.