Fixed some off-by-one bugs.