added file.close where needed