recursive solution is more elegant