Code cleanup and more tests