fixed the algorithum