Nearly functional implementation.