State machine working somewhat