Refactoring: remove code duplication