diff --git a/README.md b/README.md
index b06ffa3..08b7d14 100644
--- a/README.md
+++ b/README.md
@@ -2435,7 +2435,7 @@ def main(screen):
ch = screen.getch()
selected += (ch == KEY_DOWN) - (ch == KEY_UP)
selected = max(0, min(len(paths)-1, selected))
- first += (first <= selected - height) - (first > selected)
+ first += (selected >= first + height) - (selected < first)
if ch in [KEY_LEFT, KEY_RIGHT, KEY_ENTER, 10, 13]:
new_dir = '..' if ch == KEY_LEFT else paths[selected]
if os.path.isdir(new_dir):
diff --git a/index.html b/index.html
index 5db7fc4..8bbd5e7 100644
--- a/index.html
+++ b/index.html
@@ -1997,7 +1997,7 @@ print(table)
ch = screen.getch()
selected += (ch == KEY_DOWN) - (ch == KEY_UP)
selected = max(0, min(len(paths)-1, selected))
- first += (first <= selected - height) - (first > selected)
+ first += (selected >= first + height) - (selected < first)
if ch in [KEY_LEFT, KEY_RIGHT, KEY_ENTER, 10, 13]:
new_dir = '..' if ch == KEY_LEFT else paths[selected]
if os.path.isdir(new_dir):