Fix some bugs, improve logging