Bug fix: substring() expects length, not end position

As a result, a ']' character was left after last items in the output.