Non-recursive solution