Split out refcounting code that's about to be shared.