Lots of bugfixes and changes