Refactor variable scope