Fixed an iostream not closing bug