Refactoring repeated code