Updated UI code to a state machine