add a non-recursive solution