Why don't we just save the length in the first word? That can simplify your logic.