adopted a state machine based approach