Moved user state machine to separate function.