Fix some issues and add tests