Optimising JS and Fixing bug #2