add logging, fix some bugs