Refactor txToPairs into smaller functions

Also adds tests for those functions, and improve fee estimation.