fixed bugs with IllegalThreadStateException