Move common stuff to a function