added malloc return check and more assertions