Add exitFlag. Fix logging