Broken out a bit and added unit tests.