Lua seems a bit finicky about strings.
How about you debug each variable and each string.len etc function?

-Avi