fix several bugs, no longer crash