Improve state machine using array of functions