non-recursive solution