Bugfixes and test code