Fix close function