Redmine/6553: Fix implicit looping over multiple lists

Pull request for initial review of the changes to the code. Planning to add more test coverage to the acceptance test before merging.
I'd be happier with `return state != NULL;` here; technically no different, but makes the cast from pointer to bool explicit.