added closing of files after opening them