Fix bug with not closing files correctly.