Lots of bugfixes and small improvements