fix some logical errors and compiling errors
Merge pull request #1914 from minggo/master

fix some logical errors and compiling errors