Split for testability; add basic tests.