Fixes declaration and scope of variables