Initial work on turning everything into state machines done

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>