Tidying up the state engine