Added a closure to prevent the file variable from going out of scope