Fix off-by-one overflow and simplify