fix some bug and improve some code