lots of refactoring to fix scopes and naming