Work in progress: adding tests, restructuring to allow testing.