Refactoring redundant code