Fix off-by-one errors (fixes #4, fixes #5).