Fiexed bug in algorythm