Factor out more code into handler and state ns.