added state machine and other goodies