code cleanup and more tests