Determining a group $G$ by looking at the number of homomorphisms $H\to G$ I read somewhere that, given a finite group $G$, its structure is completely determined
from the knowledge of the values of $|\{H\to G\}|$ (the number of homomorphisms from $H$ to $G$) as $H$ varies over all finite groups. 

This has a nice corollary: if $G\times G\cong G'\times G'$ then $G\cong G'$. 

How does one prove the first assertion? If the proof is not hard, I'd prefer an answer in form of a (series of) hint(s).
 A: Maybe the following works, please tell me if this is correct. 
Let $\beta(H)$ be the number of homomorphisms $H\to G$ (which we know). 
We show that the number $\alpha(H)$ of injective homomorphisms $H\to G$ is uniquely determined by $\beta(\cdot)$, by strong induction on $n=|H|$: 
if $n=1$ then trivially $\alpha(\{e\})=1$. Now assume the thesis for $m<n$. 
We observe that, for any fixed $H$ with $|H|=n$ and any $K\trianglelefteq H$, there is an obvious correspondence between $\{\phi:H\to G\text{ s.t. Ker}(\phi)=K\}$ and $\{\psi:H/K\to G\text{ injective}\}$. So 
$\beta(H)=\alpha(H)+\sum_{\{e\}\neq K\trianglelefteq H}\alpha(H/K)$ 
and all the terms in the sum are already determined as $|H/K|<n$, hence $\alpha(H)$ is determined too. 
Now $|G|=\max\{|H|:\alpha(H)>0\}$, so $|G|$ is uniquely determined. Finally if $G,G'$ have the same function $\beta(\cdot)$, they have the same $\alpha(\cdot)$, so since $\alpha(G)>0$ there exists an injective homomorphism $G\to G'$. This is an isomorphism as $|G|=|G'|$.
