why implementing the whole logic in a functional way except for the final loop ?