Fixing variable scope