Fix scope bug in javascript