Use shared_ptr instead of managing memory manually