Fix a bunch of bugs

Description in the commit descriptions