fix some logical errors and compiling errors