Fixed resource leaks

Please review my changes. They fix several resource leaks (forgot to close a previous open file pointer).

Many thanks for considering merging

Martin Ettl