Fix tests and add tests for new functionality