fix some logic bug and some small bugs