fix off-by-one on prev_link and encoding