Fixed expandPairs tidied up other functions