fixing function scoping bug