Fixed dangling pointers problem