Refactor to improve tests, add more tests.