js code cleaning & restructuring: first cut