Check array length when searching for the end of the string

Fixes #21