Improved scope

1. significantly simplified getScope method
2. fixed logic bugs in getScope method