First wave of refactoring and test addition