working version with nested tabulate to generate pairs