Several bugfixes and small improvements