Refactor processing code.

This is much nicer to look at.