This should really be unique_ptr based, but I can't be arsed working around the lack of template typedefs here.