Fix a bunch of bugs