fix some bugs, thanks @zh-wang