diff --git a/src/orca/braille.py b/src/orca/braille.py
index 77fb522..23b29b2 100644
--- a/src/orca/braille.py
+++ b/src/orca/braille.py
@@ -36,6 +36,9 @@ log = logging.getLogger("braille")
 
 import signal
 import os
+import sys
+import pdb
+import traceback
 
 try:
     import louis
@@ -193,6 +196,10 @@ _regionWithFocus = None
 #
 viewport = [0, 0]
 
+# old offset, is used to detect if we have changed application
+# and therefore need to reset the braille display viewport.
+oldOffset = 0
+
 # The callback to call on a BrlTTY input event.  This is passed to
 # the init method.
 #
@@ -970,11 +977,18 @@ def clear():
 
     _lines = []
     _regionWithFocus = None
-    viewport = [0, 0]
+    viewport = [viewport[0], 0]
+    #print("clear()")
+    #print("stack from clear:")
+    #traceback.print_stack(file=sys.stdout)
+    #print("end stack")
+    #sys.stdout.flush()
 
 def setLines(lines):
     global _lines
     _lines = lines
+    #print("setLines")
+    #sys.stdout.flush()
 
 def addLine(line):
     """Adds a line to the logical display for painting.  The line is added to
@@ -986,6 +1000,10 @@ def addLine(line):
     - line: an instance of Line to add.
     """
 
+    #print("adding line, stack:")
+    #traceback.print_stack(file=sys.stdout)
+    #print("end of stack")
+    #sys.stdout.flush()
     _lines.append(line)
     line._index = len(_lines)
 
@@ -1012,7 +1030,11 @@ def setFocus(region, panToFocus=True, getLinkMask=True):
     global _regionWithFocus
 
     _regionWithFocus = region
+    #if not region:
+    #    pdb.set_trace()
 
+    #print("i am in setfocus, region=%s\n" %region)
+    #sys.stdout.flush()
     if not panToFocus or (not _regionWithFocus):
         return
 
@@ -1036,16 +1058,9 @@ def setFocus(region, panToFocus=True, getLinkMask=True):
         else:
             lineNum += 1
 
-    line = _lines[viewport[1]]
-    [string, offset, attributeMask] = line.getLineInfo(getLinkMask)
-
-    # If the cursor is too far right, we scroll the viewport
-    # so the cursor will be on the last cell of the display.
-    #
-    if _regionWithFocus.cursorOffset >= _displaySize[0]:
-        offset += _regionWithFocus.cursorOffset - _displaySize[0] + 1
-
-    viewport[0] = max(0, offset)
+    #print("setfocus: additional offset=%s" % _regionWithFocus.cursorOffset)
+    #sys.stdout.flush()
+    return
 
 def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True):
     """Repaints the Braille on the physical display.  This clips the entire
@@ -1073,19 +1088,15 @@ def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True):
     global endIsShowing
     global beginningIsShowing
     global cursorCell
-    global _monitor
+
+    #print("refresh: pc=%s, tc=%s,lm=%s\n" \
+    #  %(panToCursor, targetCursorCell, getLinkMask))
+    #sys.stdout.flush()
 
     if len(_lines) == 0:
-        if not _brlAPIRunning:
-            init(_callback, settings.tty)
-        if _brlAPIRunning:
-            try:
-                _brlAPI.writeText("", 0)
-            except:
-                debug.println(debug.LEVEL_WARNING,
-                              "BrlTTY seems to have disappeared:")
-                debug.printException(debug.LEVEL_WARNING)
-                shutdown()
+        #print("had no braille to print out\n")
+        #sys.stdout.flush()
+        writeBraille()
         return
 
     # Now determine the location of the cursor.  First, we'll figure
@@ -1100,33 +1111,46 @@ def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True):
     # actually is in the string.
     #
     line = _lines[viewport[1]]
-    [string, focusOffset, attributeMask] = line.getLineInfo(getLinkMask)
-    cursorOffset = -1
-    if focusOffset >= 0:
-        cursorOffset = focusOffset + _regionWithFocus.cursorOffset
+    #print("looking at line %s, its: %s" %(viewport[1], line))
+    #sys.stdout.flush()
+    #pdb.set_trace()
+    [string, offset, attributeMask] = line.getLineInfo(getLinkMask)
 
-    # Now, if desired, we'll automatically pan the viewport to show
-    # the cursor.  If there's no targetCursorCell, then we favor the
-    # left of the display if we need to pan left, or we favor the
-    # right of the display if we need to pan right.
+    additionalOffset = -1
+    if offset >= 0:
+        additionalOffset  = _regionWithFocus.cursorOffset 
+
+    # If the cursor is too far right, we scroll the viewport
     #
-    if panToCursor and (cursorOffset >= 0):
-        if len(string) <= _displaySize[0]:
-            viewport[0] = 0
-        elif targetCursorCell:
-            viewport[0] = max(0, cursorOffset - targetCursorCell + 1)
-        elif cursorOffset < viewport[0]:
-            viewport[0] = max(0, cursorOffset)
-        elif cursorOffset >= (viewport[0] + _displaySize[0]):
-            viewport[0] = max(0, cursorOffset - _displaySize[0] + 1)
+    totalOffset = offset + additionalOffset
+
+    #print("of=%s, ao=%s, to=%s, vp=%s, str=%s\n" \
+    #  %(offset, additionalOffset, totalOffset, viewport[0], string))
+    #sys.stdout.flush()
+    if panToCursor:
+        if settings.braillePanningStyle == settings.PAN_BRAILLE_AS_BRLTTY:
+            #print("refreshing as brltty")
+            #sys.stdout.flush()
+            _refreshAsBrlTTY(string, offset, additionalOffset, targetCursorCell)
+        elif settings.braillePanningStyle == settings.PAN_BRAILLE_BY_WORD:
+            #print("frefreshing by word")
+            #sys.stdout.flush()
+            _refreshByWords(string, offset, additionalOffset, targetCursorCell)
+        else:
+            #print("refreshing as original")
+            #sys.stdout.flush()
+            _refreshAsOriginal(string, offset, additionalOffset, \
+              targetCursorCell)
 
     startPos = viewport[0]
     endPos = startPos + _displaySize[0]
+    #print("after: start=%s, end=%s\n" %(startPos, endPos))
+    #sys.stdout.flush()
 
     # Now normalize the cursor position to BrlTTY, which uses 1 as
     # the first cursor position as opposed to 0.
     #
-    cursorCell = cursorOffset - startPos
+    cursorCell = totalOffset - startPos
     if (cursorCell < 0) or (cursorCell >= _displaySize[0]):
         cursorCell = 0
     else:
@@ -1139,21 +1163,26 @@ def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True):
                     (string[startPos:endPos], cursorCell)
     debug.println(debug.LEVEL_INFO, logLine)
     log.info(logLine.encode("UTF-8"))
-
     substring = string[startPos:endPos]
+    submask = attributeMask[startPos:endPos]
+    writeBraille(substring, submask)
+    #print("display:%s\n" %substring)
+    #sys.stdout.flush()
+    beginningIsShowing = startPos == 0
+    endIsShowing = endPos >= len(string)    
+
+def writeBraille(string = "", attributeMask = ""):
+    global _monitor
+
     if not _brlAPIRunning:
         init(_callback, settings.tty)
+
     if _brlAPIRunning:
         writeStruct = brlapi.WriteStruct()
         writeStruct.regionBegin = 1
-        writeStruct.regionSize = len(substring)
-        while writeStruct.regionSize < _displaySize[0]:
-            substring += " "
-            if attributeMask:
-                attributeMask += '\x00'
-            writeStruct.regionSize += 1
-        writeStruct.text = substring
-        writeStruct.cursor = cursorCell
+        writeStruct.regionSize = _displaySize[0]
+        blankLength = _displaySize[0] - len(string.decode("UTF-8"))
+        string += " " * blankLength
 
         # [[[WDW - if you want to muck around with the dots on the
         # display to do things such as add underlines, you can use
@@ -1171,34 +1200,31 @@ def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True):
         #writeStruct.attrOr = myUnderline
 
         if attributeMask:
-            writeStruct.attrOr = attributeMask[startPos:endPos]
+            attributeMask += '\x00' * blankLength
+        writeStruct.text = string
+        writeStruct.cursor = cursorCell
+        writeStruct.charset = "UTF-8"
+        if attributeMask:
+            writeStruct.attrOr = attributeMask
 
-        if not _brlAPIRunning:
-            init(_callback, settings.tty)
-        if _brlAPIRunning:
-            try:
-                _brlAPI.write(writeStruct)
-            except:
-                debug.println(debug.LEVEL_WARNING,
-                              "BrlTTY seems to have disappeared:")
-                debug.printException(debug.LEVEL_WARNING)
-                shutdown()
+        try:
+            _brlAPI.write(writeStruct) 
+        except:
+            debug.println(debug.LEVEL_WARNING,
+                          "BrlTTY seems to have disappeared:")
+            debug.printException(debug.LEVEL_WARNING)
+            shutdown()
+            return
 
     if settings.enableBrailleMonitor:
         if not _monitor:
             _monitor = brlmon.BrlMon(_displaySize[0])
             _monitor.show_all()
-        if attributeMask:
-            subMask = attributeMask[startPos:endPos]
-        else:
-            subMask = None
-        _monitor.writeText(cursorCell, substring, subMask)
+        _monitor.writeText(cursorCell, string, attributeMask)
     elif _monitor:
         _monitor.destroy()
         _monitor = None
-
-    beginningIsShowing = startPos == 0
-    endIsShowing = endPos >= len(string)
+    return
 
 def displayRegions(regionInfo):
     """Displays a list of regions on a single line, setting focus to the
@@ -1222,6 +1248,164 @@ def displayRegions(regionInfo):
     setFocus(focusedRegion)
     refresh()
 
+def _refreshByWords(string, offset, additionalOffset, targetCursorCell):
+    """Helper function for refresh()
+    
+    This function positions full words at the edge of the display, if
+    the cursor is within margin of the edge.  If the word to be
+    displayed is considered to be too long (larger than maxJump), then
+    we only reposition display by maxJump.
+    """
+
+    global oldOffset
+    global viewport
+
+    # How close the braille cursor is allowed to the edge of the
+    # braille display.
+    #
+    margin = 3 
+
+    # How far we are allowed to look for the next word (beyond the
+    # edge of the braille display)
+    #
+    maxJump = 8+margin
+    newstring = string.rstrip()
+
+    # print("string was %d chars long, and has become %d" \
+    #       % (len(string), len(newstring)))
+    string = newstring
+    totalOffset = offset + additionalOffset
+    # if totalOffset > len(string):
+    #     string = string + " " * ((totalOffset - len(string)) + margin)
+    #     print("string expanded to %d" % len(string))
+
+    # Checking if our offset has changed,
+    # which probably means that we have changed application.
+    #
+
+    if oldOffset != offset:
+        viewport[0] = oldOffset = offset
+
+    # Calculating absolute values:
+    #
+    innerLeftMargin  = viewport[0] + margin - 1
+    outerLeftMargin = string[:viewport[0]].rfind(' ')
+
+    # Varifying that we are not jumping the display more than allowed.
+    #
+    if outerLeftMargin < viewport[0] - maxJump:
+        outerLeftMargin = viewport[0] - maxJump
+
+    rightDisplayEdge = viewport[0] + _displaySize[0]
+    innerRightMargin = rightDisplayEdge  - margin
+    outerRightMargin = string[rightDisplayEdge:].find(' ') \
+      + rightDisplayEdge
+
+    # Verifying that we are not jumping the display more than allowed.
+    #
+    if outerRightMargin > rightDisplayEdge + maxJump:
+        outerRightMargin = rightDisplayEdge + maxJump
+
+    #print("b: vp=%d,of=%d,to=%d,ol=%d,il=%d,ir=%d,or=%d" \
+    #       % (viewport[0],offset,totalOffset,outerLeftMargin,innerLeftMargin,
+    #          innerRightMargin,outerRightMargin))
+
+    # Working out where to position the display.
+    # Warning, these if blocks are order dependant.
+    #
+    if targetCursorCell:
+        viewport[0] = max(0, totalOffset - targetCursorCell + 1)
+    elif totalOffset >= outerRightMargin:
+        viewport[0] = max(0, totalOffset - _displaySize[0]/2 + 1)
+        #print("branch 1")
+    elif totalOffset >= innerRightMargin and \
+        outerRightMargin  >= rightDisplayEdge:
+        outerRightMargin = string[outerRightMargin+1:].find(' ') \
+          + outerRightMargin + 1
+        if outerRightMargin  > rightDisplayEdge + maxJump:
+            outerRightMargin  = rightDisplayEdge + maxJump
+        viewport[0] = max(0, (outerRightMargin - _displaySize[0]))
+        #print("branch 2")
+    elif outerLeftMargin <= offset and totalOffset <= rightDisplayEdge:
+        if totalOffset < _displaySize[0]:
+            viewport[0] = 0
+        else:
+            viewport[0] =  max(0, offset)
+            #print("branch 3")
+    # elif totalOffset <= outerLeftMargin \
+    #      and (totalOffset - offset) < _displaySize[0]:
+    #     viewport[0] =  max(0, offset)
+    #     # print("branch 3.5")
+    elif totalOffset <= outerLeftMargin:
+        viewport[0] = max(0, totalOffset)
+        #print("branch 4")
+    elif totalOffset <= innerLeftMargin:
+        outerLeftMargin = string[:outerLeftMargin-1].rfind(' ') + 1
+        if outerLeftMargin  < (viewport[0] - maxJump):
+            outerLeftMargin  = viewport[0] - maxJump
+        viewport[0] = max(0, outerLeftMargin)
+        #print("branch 5")
+    #print("a: vp=%d,of=%d,to=%d,ol=%d,il=%d,ir=%d,or=%d" \
+    #       % (viewport[0],offset,totalOffset,
+    #          outerLeftMargin,innerLeftMargin,
+    #          innerRightMargin,outerRightMargin))
+    return
+
+def _refreshAsBrlTTY(string, offset, additionalOffset, targetCursorCell):
+    """Helper function for refresh()
+    
+    This function simulates brlttys method of breaking text.
+    """
+
+    global viewport
+
+    # If the totalOffset is less than displaysize, 
+    # then we wish it to be context and has to be shown.
+
+    totalOffset = offset + additionalOffset
+    if totalOffset < _displaySize[0]:
+        viewport[0] = 0
+    else:
+        fullDisplays = additionalOffset / _displaySize[0]
+        viewport[0] = offset + fullDisplays * _displaySize[0]
+    return
+
+def _refreshAsOriginal(string, offset, additionalOffset, targetCursorCell):
+    if offset >= 0:
+        additionalOffset  += offset
+
+    # Now, if desired, we'll automatically pan the viewport to show
+    # the cursor.  If there's no targetCursorCell, then we favor the
+    # left of the display if we need to pan left, or we favor the
+    # right of the display if we need to pan right.
+    #
+    #print("orig: of=%s, ao=%s, vp=%s" %(offset, additionalOffset, viewport[0]))
+    #sys.stdout.flush()
+    if additionalOffset >= 0:
+        if len(string) <= _displaySize[0]:
+            #print("orig: string.len <= displaysize")
+            #sys.stdout.flush()
+            viewport[0] = 0
+        elif targetCursorCell:
+            #print("orig: target cursorcell")
+            #sys.stdout.flush()
+            viewport[0] = max(0, additionalOffset  - targetCursorCell + 1)
+        elif additionalOffset  < viewport[0]:
+            #print("additional < viewport")
+            #sys.stdout.flush()
+            viewport[0] = max(0, additionalOffset )
+        elif offset >= (viewport[0] + _displaySize[0]):
+            #print("offset >= vp+ao")
+            #sys.stdout.flush()
+            viewport[0] = max(0, offset)
+        elif additionalOffset  >= (viewport[0] + _displaySize[0]):
+            #print("additional >= viewport+ao")
+            #sys.stdout.flush()
+            viewport[0] = max(0, additionalOffset  - _displaySize[0] + 1)
+    #print("orig: returning, vp=%s" % viewport[0])
+    #sys.stdout.flush()
+    return
+
 def displayMessage(message, cursor=-1):
     """Displays a single line, setting the cursor to the given position,
     ensuring that the cursor is in view.
diff --git a/src/orca/orca_prefs.py b/src/orca/orca_prefs.py
index d21ea86..69ec58b 100644
--- a/src/orca/orca_prefs.py
+++ b/src/orca/orca_prefs.py
@@ -313,6 +313,16 @@ class OrcaPrefs:
         else:
             return "orca.settings.BRAILLE_ROLENAME_STYLE_LONG"
 
+    def _getBraillePanningStyleString(self, braillePanningStyle):
+        """Returns a string that represents the braillePanningStyle passed."""
+
+        if braillePanningStyle == settings.PAN_BRAILLE_AS_BRLTTY:
+            return "orca.settings.PAN_BRAILLE_AS_BRLTTY"
+        elif braillePanningStyle == settings.PAN_BRAILLE_BY_WORD:
+            return "orca.settings.PAN_BRAILLE_BY_WORD"
+        else:
+            return "orca.settings.PAN_BRAILLE_AS_ORIGINAL"
+
     def _getVerbalizePunctuationStyleString(self, punctuationStyle):
         """Returns a string that represents the punctuation style passed in."""
 
@@ -685,6 +695,8 @@ class OrcaPrefs:
                 value = self._getBrailleSelectionIndicatorString(prefsDict[key])
             elif key == "brailleLinkIndicator":
                 value = self._getBrailleLinkIndicatorString(prefsDict[key])
+            elif key == "braillePanningStyle":
+                value = self._getBraillePanningStyleString(prefsDict[key])
             elif key == "verbalizePunctuationStyle":
                 value = self._getVerbalizePunctuationStyleString(prefsDict[key])
             elif key == "sayAllStyle":
diff --git a/src/orca/scripts/toolkits/Gecko/script.py b/src/orca/scripts/toolkits/Gecko/script.py
index 2721544..9578145 100644
--- a/src/orca/scripts/toolkits/Gecko/script.py
+++ b/src/orca/scripts/toolkits/Gecko/script.py
@@ -2379,7 +2379,8 @@ class Script(default.Script):
 
         if extraRegion:
             line.addRegion(extraRegion)
-
+        if not focusedRegion:
+            focusedRegion = regions[0]
         braille.setFocus(focusedRegion, getLinkMask=False)
         braille.refresh(panToCursor=True, getLinkMask=False)
 
diff --git a/src/orca/settings.py b/src/orca/settings.py
index e2d2bac..2ce86d8 100644
--- a/src/orca/settings.py
+++ b/src/orca/settings.py
@@ -115,6 +115,7 @@ userCustomizableSettings = [
     "brailleRolenameStyle",
     "brailleSelectorIndicator",
     "brailleLinkIndicator",
+    "braillePanningStyle",
     "enableBrailleMonitor",
     "enableMagnifier",
     "enableMagLiveUpdating",
@@ -267,6 +268,13 @@ BRAILLE_LINK_8    = 0x80 # 10000000
 BRAILLE_LINK_BOTH = 0xc0 # 11000000
 brailleLinkIndicator = BRAILLE_LINK_BOTH
 
+# braille display panning styles
+PAN_BRAILLE_BY_WORD = 1
+PAN_BRAILLE_AS_BRLTTY = 2
+PAN_BRAILLE_AS_ORIGINAL = 3
+
+braillePanningStyle = PAN_BRAILLE_AS_ORIGINAL
+
 # Speech punctuation levels (see verbalizePunctuationStyle).
 #
 PUNCTUATION_STYLE_NONE = 3
