Lots and lots of bugfixes