Fixing an unstable Javascript condition