# Buggy code
def add_flow(self, f):
    super(ConsoleState, self).add_flow(f)
    if self.focus is None:
        self.set_focus(0)
    elif self.follow_focus:
        self.set_focus(len(self.view) - 1)
    self.set_flow_marked(f, False)
    return f


# Traceback (IndexError)
Traceback (most recent call last):
File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 498, in run
self.loop.run()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 715, in _loop
alarm_callback()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 164, in cb
callback(self, user_data)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 451, in ticker
self.loop.draw_screen()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 578, in draw_screen
canvas = self._topmost_widget.render(self.screen_size, focus=True)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
canv = fn(self, size, focus=focus)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/container.py", line 1083, in render
focus and self.focus_part == 'body')
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
canv = fn(self, size, focus=focus)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/decoration.py", line 225, in render
canv = self._original_widget.render(size, focus=focus)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
canv = fn(self, size, focus=focus)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 457, in render
(maxcol, maxrow), focus=focus)
File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 342, in calculate_visible
focus_widget, focus_pos = self.body.get_focus()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/flowlist.py", line 288, in get_focus
f, i = self.state.get_focus()
File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 69, in get_focus
return self.view[self.focus], self.focus
File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/flow.py", line 383, in __getitem__
return self._list[item]
IndexError: list index out of range

# Fixed code
def add_flow(self, f):
    super(ConsoleState, self).add_flow(f)
    if self.focus is None:
        self.set_focus(0)
    elif self.follow_focus:
        self.update_focus()
    self.set_flow_marked(f, False)
    return f