Moved file closure to the proper location, after end