fix minor remarks
move cleanup_logfile to an inner function, clean up end cleanup