Fixed some bugs in JS version and added JS tests