Write tests, update state machine, make it less complex