fix a few off by one bugs