Fixed close function