Algorithm fixed, so it passes all written tests.