Move declaration of variable to top of function.
Define modes clearly.