A useful refactoring tool would be a multiplexer. This is a chip that takes multiple inputs, and chooses one value from those inputs to output on a single output, where that choice is made by the value of a selector pin.