re-structure javascripts to remove race cond error