Split the state handling function into several per-state functions.