Fixed dangling pointer issue