Improve logging and fix some minor bugs