Fix loop state parsing

Each loop has a separate valid state flag, so rather than using a global
flag we now set and query each individual loop's flag.
Additional 0-size checking