Fix the segfault problem and the state problem