Added checks for malloc() failures