Fixes a bunch of bugs that cause 500 errors.