Extracted code from Main into Gui.