making close() more robust