fix some bugs in last patch and add logging