Moved variable declarations to top of function.
Added link to spec for reference.