Add test case to verify bug
Fix off-by-one error