code cleanup and tests.