fix variable scope problem