Fixed a bunch of bugs after refactoring.