refactoring: factor out common code