integration of better dynamic scoping techniques and refactor