Bugfixes

A bunch of stuff fixed up by andrew