Several bugfixes