Fixed *almost* all of the test-detected bugs