Fixed some bugs in the JS code