added elegant recursive solution