Some variable scope fixes