Simplify for easier debugging and add tests.