some shared code extracted