Add test helper and a bunch of tests
More cleanups, and fixes for various bugs