Added some malloc checks