hopefully fixed WRITEs and added some CLOSE stuff