fix potential bugs by using smart pointers.