Code refactoring to make it DRY