Template state machine for general uses.