fix an infinit loop bug but still get wrong answer