properly open and close files