move refcount back to main structs; code factoring