Added counter to the algorithm