minor refactoring - uses local variables instead of global