Fixed bug, passes more test cases.