bug, refactoring, unneeded imports

Hey,

Saw your code in r/python and found a bug, some missing file.close() methods that I refactored to with blocks and a couple of unneeded statements so I thought I would try my hand at this whole open source pull request thing. Keep up the nice work!