Fix some issues and add some tests.