qdm2: check array index before use, fix out of array accesses