bug fix - closed file too early