fix several fatal bugs