Variable scope fixes