Close files explicitly

This should prevent "OSError: Too many open files" errors. I actually got this error after creating many models.