Fixes a lot of bugs