tidy up rather half-baked js code