Use std::unique_ptr to simplify this code a bit.