refactoring: extract some common code