added state machine stuff