Fixing var scope issue