static String readCommand(String cmd) throws Exception {
        boolean error = false;
        cmd = cmd.replace(" ", "");

        // Check if only allowed chars
        if(Pattern.matches(PATTERN, cmd)) {

            // This chunk here checks balancing brackets
            LinkedList<Character> stack = new LinkedList<>();
            for(int i = 0; i < cmd.length(); i++) {
                if(cmd.charAt(i) == '[') {
                    stack.addLast(cmd.charAt(i));
                }
                if(cmd.charAt(i) == ']') {
                    if(stack.isEmpty()) {
                        error = true;
                    } else {
                        if(stack.removeLast() != '[') {
                            error = true;
                        }
                    }
                }
            }

            if(!stack.isEmpty()) {
                error = true;
            }

            if(error) {
                throw new Exception("Error - Illegal balancing character ie not a corresponding closing-bracket ']'");
            } else {
                return cmd;
            }

        } else {
            throw new Exception("Error - Illegal characters. Type 'help' for a list of commands");
        }
    }