substr out of range fix