Explicitly close the open file
Refactor to use class level instance variables
Ensure close and explicitly unlink as per best practice.