public static Block loadSim(InputStream in) {
        BufferedReader reader = null;
        try {
            Program program = new Program();
            reader = new BufferedReader(new InputStreamReader(in));
            InstructionTree tree = program.getInstructions();
            Deque<InstructionBranch> branches = new LinkedBlockingDeque<>();
            branches.add(tree.getStartInstruction());
            while (reader.ready()) {
                String line = reader.readLine();
                parseLine(program, line, branches);
            }
            //System.out.println(program.toString());
            reader.close();
            String blockId = program.getVariable("$id");
            String blockName = program.getVariable("$name");
            //System.out.println(blockId + " / " + blockName);
            if (blockId != null) program.setId(blockId);
            if (blockName != null) program.setName(blockName);
            Sim sim = new Sim(program);
            SimResult result = sim.startSim();
            SimState endState = result.getEndState();
            if (endState != SimState.FINISHED) {
                throw new RuntimeException("Sim finished unexpectedly with state " + endState.getState());
            }
            return result.getBlock();
        } catch (Exception e) {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ignored) {}
            throw new RuntimeException("Exception loading sim!", e);
        }
    }