implement scoping algorithm