fix bug and improve thread