using functional algorithm instead