private void rewind() {
        try {
            if (scriptEnded || programHalted) {
                gui.enableSingleStep();
                gui.enableFastForward();
            }
            scriptEnded = false;
            programHalted = false;

            int oldAnimationMode = animationMode;
            setAnimationMode(DISPLAY_CHANGES);
            simulator.restart();
            refreshSimulator();
            setAnimationMode(oldAnimationMode);

            if (output != null)
                resetOutputFile();
            if (comparisonFile != null)
                resetComparisonFile();

            lastEcho = "";
            currentCommandIndex = 0;
            gui.setCurrentScriptLine(script.getLineNumberAt(0));

        } catch (ControllerException e) {
            displayMessage(e.getMessage(), true);
        }
    }