Some changes to the algorithm