issue #10, extract common code logic