lab4v2: another bunch of bugs, but NOW algorithm is correct