added changes to destroy file and closed handles if cases fail