Refactor common functionality in the TwoSided factors to AbstractTwoSidedFactor.java