Significant rewrite: reduced scope, slight rearch, and added proper unit test coverage.