added logfile, cleaned up code and comments