Move setting the refcount to the end of the function