Ensure file handles are always closed