add gotoIdleState function and fix minor bugs