refactored scoping a bit