This seems like a non-trivial change to this logic. Please write some tests that cover all of the cases that were failing or broken before in `test_qapply.py`.