Moved closing file into if statement.