Further refactoring and duplicate removal.