Fixed a whole bunch of bugs