Add logfile and change main function position