Some refactorings to remove duplicate code