updated close function for safety