Brought in some things from prison g.

and some variable scope fixes that were (maybe) causing odd behavior