Common code cleanup + more tests.