A more robust fix for #319. This version prevents overscroll and works with the visual modes.