Refactoring to get rid of duplicate code.