Fix off-by-one bug. Improve JSON output.