Fix some more off-by-one error

Cleanup some code