Added a functional implementation. It's significantly slower.