Some of these variables were re-used in later code, so moving them outside of the if logic broke things.