Test improvements and bugfixes