fix off-by-one
fix hoisted variable assignment