added some memory cleanup to return xml