Keep it DRY, refactoring a piece of code repeated 3 times