Added checks for failed allocations