Fixed an off-by-one error, corrected agent hooks.