Implementation without recursion