Longer term, it looks like the function signature could be simplified to just take `ap1`, `ap2`, `sorter`, and `ret`, with the function itself pulling out the relevant data from those. It's a bit more code repetition, but I think a simple interface may be the better tradeoff.