Better reference removal in serialization function