this does the whole work.

probably should add timings, but it works.