More cleanups and unit tests