Fixed a bunch of compiler warnings and bugs