Split out process function to module for easier reuse

Review please. Merge if you want, but more commits coming.