Fix segfault and modify state struct