More unit tests and cleanups