Added a statebased system so that everything is done in one window