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.
(minor clarity issue) result.rval.item is NULL, given the else-if's condition (I should have noticed this before !) so it'd be clearer to simply write seq = NULL here.