Refactor txtopairs into smaller functions

Also adds a test for one of the new functions