State machine coding problem