Refactored processing flow (almost done, but not tested)