Tests. Several bugfixes.