refactoring: extract common code