Some more fixes, cleanups and tests.