Moved code around to prevent warnings about undeclared stuff.
Typo corrected.