Fixing awesome bug in JavaScript