private void insertString(int offset, String str, AttributeSet a) {
        if (chatPane.getStyledDocument().getLength() == 0) str = str.trim() + " "; //removes \n at start of document

        int extent = chatScrollPane.getVerticalScrollBar().getModel().getExtent();
        int max = chatScrollPane.getVerticalScrollBar().getModel().getMaximum();
        int val = chatScrollPane.getVerticalScrollBar().getModel().getValue();
        boolean end = ((val + extent == max) || val + extent > max - 15);

        try {
            chatPane.getStyledDocument().insertString(offset, str, a);
            if (!GUI.disableTabNotificationsGlobally) checkForActiveTab();
        }
        catch (BadLocationException ignored){}

        try {
            if (chatPane.getStyledDocument().getText(0,chatPane.getStyledDocument().getLength()).equals(" ")){
                chatPane.getStyledDocument().remove(0, chatPane.getStyledDocument().getLength());
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        //controls autoscrolling -- if scrolled to bottom then scroll to end, otherwise don't
        if (end) {
            if (chatPane.getDocument().getLength() == 0) return;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        chatPane.getCaret().setDot(chatPane.getStyledDocument().getLength());
                    } catch (Exception ignored) {}
                }
            });
        }
    }