Refactoring into cpan module

Work to refactor this into something CPAN-releasable and testable.