Fix writing one byte beyond screen buffer.

This fixes a > 10yr old bug writing one byte beyond the screen buffer.
It also add a small optimization.