fix off-by-one bug with diff calculation