fixed error in algorithm