created subfunctions to print logfile and switched to lexical filehandles