add some more NULL-pointer-checks; free allocated mem in case of OOM.