Added cleanup to function.