Reusing endpos to avoid another call to strlen