roughed out a state machine based on code stolen from stack overflow