second algorithm made to work