Extract some common code