Rewrote the main loop and added some more log messages