Rewrite Algorithm and Fix Error in TestCase