Refactorings to get rid of duplicate code