Many bugfixes and changes