Some bugfixes and minor improvements