extract some common functions