pure functional implementation.