Fix lots of bugs