fixing function variable declaration and scope referencing