First cut of the extraction. Still a lot of clean-up to be done, before refactoring it into something useful.