fix variable scope issue