fix some off by one overflows n bugs