Refactor StateMachine to simplify transitioning

Also I added a `refresh` method to it that refreshes the screen.