Reestructure again to be easier to test and to remove duplication