lab2: change algorithm to more explicit