Added second pass algorithm